﻿#define  _CRT_SECURE_NO_WARNINGS
#include"Snake.h"

void SetPos(int x, int y)
{
	//获取标准输出设备的句柄
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	//定位光标位置
	COORD pos = { x, y };
	SetConsoleCursorPosition(houtput, pos);
}
void WelcomeToGame()
{
	SetPos(38, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(40, 20);
	system("pause");
	system("cls");
	SetPos(24, 14);
	wprintf(L"用 ↑ . ↓ . ← . → 分别控制蛇的移动，F3为加速，F4为减速\n");
	SetPos(24, 15);
	wprintf(L"加速可以得到更高的分数\n");
	SetPos(40, 20);
	system("pause");
	system("cls");
}
void CreateMap()
{
	//上
	SetPos(0, 0);
	for(int i = 0; i < 29; i++)
		wprintf(L"%lc", WALL);
	//下
	SetPos(0, 26);
	for (int i = 0; i < 29; i++)
		wprintf(L"%lc", WALL);
	//左右
	for (int i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}
void InitSnake(pSnake pS)
{
	//蛇身默认5节，创建5个结点
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (!cur)
		{
			perror("InitSnake:: malloc error!\n");
			return;
		}
		cur->next = NULL;
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;

		//头插法插入链表
		if (pS->_pSnake == NULL)
		{
			//为空
			pS->_pSnake = cur;
		}
		else
		{
			//不为空 
			cur->next = pS->_pSnake;
			pS->_pSnake = cur;
		}
	}

	//在地图上打印蛇身
	cur = pS->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	//设置贪吃蛇的属性
	pS->_dir = RIGHT;//默认向右走
	pS->_score = 0;
	pS->_food_weight = 10;
	pS->_sleep_time = 200;//单位：毫秒
	pS->_status = OK;
}
void CreateFood(pSnake pS)
{
	int x = 0, y = 0;
	//生成食物坐标
again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	//食物坐标不能与蛇身重合
	pSnakeNode cur = pS->_pSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
		{
			goto again;
		}
		cur = cur->next;
	}

	//创建食物的节点
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (!pFood)
	{
		perror("CreateFood()::malloc()");
		return;
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;

	//打印食物
	SetPos(pFood->x, pFood->y);
	wprintf(L"%lc", FOOD);
	pS->_pFood = pFood;
}
void GameStart(pSnake pS)
{
	//初始化游戏
	setlocale(LC_ALL, "");//适配本地环境
	srand((unsigned int)time(NULL));//将随机数的种子设置为时间戳
	//0. 设置窗口大小、设置窗口名称、光标隐藏
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出流的句柄
	CONSOLE_CURSOR_INFO CursorInfo;//创建光标结构体变量
	GetConsoleCursorInfo(houtput, &CursorInfo);//获取控制台上的光标信息
	CursorInfo.bVisible = false;//调整光标可见度为false
	SetConsoleCursorInfo(houtput, &CursorInfo);//设置控制台光标状态
	
	//1.打印欢迎界面与功能介绍
	WelcomeToGame();
	
	//2.绘制地图
	CreateMap();
	
	//3.创建蛇
	InitSnake(pS);
	
	//4.创建食物
	CreateFood(pS);
}
void PrintHelpInfo()
{
	SetPos(64, 16);
	wprintf(L"%ls", L"不能穿墙，不能咬到自己");
	SetPos(64, 17);
	wprintf(L"%ls", L"用 ↑ ↓ ← → 来控制蛇的移动");
	SetPos(64, 18);
	wprintf(L"%ls", L"按F3加速，F4减速");
	SetPos(64, 19);
	wprintf(L"%ls", L"按ESC退出游戏，按空格暂停游戏");
	SetPos(64, 21);
	wprintf(L"%ls", L"David&猪大卫 制作");
}

#define KEY_PRESS(vk) ((GetAsyncKeyState(vk) & 1)? 1: 0)
void Pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
int NextIsFood(pSnakeNode pN, pSnake pS)
{
	return (pS->_pFood->x == pN->x && pS->_pFood->y == pN->y);
}
void EatFood(pSnakeNode pN, pSnake pS)
{
	//头插法
	pN->next = pS->_pSnake;
	pS->_pSnake = pN;
	//释放食物节点
	free(pS->_pFood);
	pS->_pFood = NULL;
	//打印蛇
	pSnakeNode cur = pS->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	pS->_score += pS->_food_weight;
	//重新生成食物
	CreateFood(pS);
}
void NoFood(pSnakeNode pN, pSnake pS)
{
	//头插法
	pN->next = pS->_pSnake;
	pS->_pSnake = pN;
	//覆盖多余蛇身并释放对应节点
	pSnakeNode prev = pS->_pSnake;
	while (prev->next->next != NULL)
	{
		SetPos(prev->x, prev->y);
		wprintf(L"%lc", BODY);
		prev = prev->next;
	}
	SetPos(prev->next->x, prev->next->y);
	printf("  ");
	free(prev->next);
	prev->next = NULL;
}
void KillByWall(pSnake pS)
{
	if (pS->_pSnake->x == 0 || pS->_pSnake->x == 56 ||
		pS->_pSnake->y == 0 || pS->_pSnake->y == 26)
	{
		pS->_status = KILL_BY_WALL;
	}
}
void KillBySelf(pSnake pS)
{
	pSnakeNode cur = pS->_pSnake->next;
	while (cur)
	{
		if (cur->x == pS->_pSnake->x && cur->y == pS->_pSnake->y)
		{
			pS->_status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}
void SnakeMove(pSnake pS)
{
	//创建蛇将要到的下一个节点
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (!pNextNode)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (pS->_dir)
	{
	case UP:
		pNextNode->x = pS->_pSnake->x;
		pNextNode->y = pS->_pSnake->y - 1;
		break;
	case DOWN:
		pNextNode->x = pS->_pSnake->x;
		pNextNode->y = pS->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = pS->_pSnake->x - 2;
		pNextNode->y = pS->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = pS->_pSnake->x + 2;
		pNextNode->y = pS->_pSnake->y;
		break;
	}

	//判断下一节点是否遇到食物
	if (NextIsFood(pNextNode, pS))
	{
		//是食物
		EatFood(pNextNode, pS);
	}
	else
	{
		//不是食物
		NoFood(pNextNode, pS);
	}
	
	//检测蛇是否撞墙或撞到自己
	KillByWall(pS);
	KillBySelf(pS);
}
void GameRun(pSnake pS)
{
	//打印帮助信息
	PrintHelpInfo();
	//检测按键
	do
	{
		//打印 总分 与 食物分值
		SetPos(64, 11);
		printf("总分数：%d\n", pS->_score);
		SetPos(64, 12);
		printf("当前食物的分值：%2d", pS->_food_weight);
		if (KEY_PRESS(VK_UP) && pS->_dir != DOWN)
		{
			pS->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && pS->_dir != UP)
		{
			pS->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && pS->_dir != RIGHT)
		{
			pS->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && pS->_dir != LEFT)
		{
			pS->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//暂停
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//退出
			pS->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (pS->_food_weight < 18)
			{
				pS->_sleep_time -= 30;
				pS->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (pS->_food_weight > 2)
			{
				pS->_sleep_time += 30;
				pS->_food_weight -= 2;
			}
		}
		//蛇走一步的过程
		SnakeMove(pS);
		Sleep(pS->_sleep_time);
	} while (pS->_status == OK);

}
void GameEnd(pSnake pS)
{
	SetPos(20, 12);
	switch (pS->_status)
	{
	case END_NORMAL:
		wprintf(L"您主动结束了游戏\n");
		break;
	case KILL_BY_WALL:
		wprintf(L"您撞到了墙上，游戏结束\n");
		break;
	case KILL_BY_SELF:
		wprintf(L"您撞到了自己，游戏结束\n");
		break;
	}
	//释放蛇身的链表
	pSnakeNode cur = pS->_pSnake;
	pSnakeNode del = NULL;
	while (cur)
	{
		del = cur;
		cur = cur->next;
		free(del);
	}
}